<!DOCTYPE html>
<html lang="zh">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>QQ 验证码登录</title>
    <link rel="stylesheet" href="css/qq_login.css">
</head>
<body>
<div id="qqLoginDiv">
    <h2>QQ 验证码登录</h2>
    <div id="msg">{{ msg }}</div>
    <div class="form-group">
        <label for="qqNumber">QQ号：</label>
        <input type="text" id="qqNumber" placeholder="请输入 QQ 号" v-model="qqNumber">
    </div>
    <div class="form-group">
        <button class="button" id="requestCodeBtn" @click="requestCode">申请验证码</button>
    </div>
    <div class="form-group">
        <label for="verificationCode">验证码：</label>
        <input type="text" id="verificationCode" placeholder="请输入验证码" v-model="verificationCode">
    </div>
    <div class="form-group">
        <button class="button" id="submitCodeBtn" @click="qqlogin">提交验证码</button>
    </div>
    <br>
    <div class="form-group">
        <input type="button" id="tologin" value="用户名密码登录" @click="tologin">
    </div>
</div>

<script src="js/axios-0.18.0.js"></script>
<script src="js/vue.js"></script>
<script>
    new Vue({
        el: '#qqLoginDiv',
        data: {
            qqNumber: '2171708754',
            verificationCode: '',
            msg: ''
        },
        methods: {
            //申请验证码
            requestCode() {
                axios.get("http://localhost:8080/brand-case/user/getQQCode", {
                    params: {
                        qqNumber: this.qqNumber
                    }
                }).then(resp => {
                    if (resp.data === "toofrequent") {
                        alert("验证码请求过于频繁，请稍后再试！");
                    } else {
                        console.log(resp.data)
                        alert("您的qq验证码为 :" + resp.data);
                    }
                }).catch(error => {
                    console.error("请求验证码失败：", error);
                    alert("请求验证码失败，请检查网络或联系管理员。");
                });
            },
            qqlogin() {
                axios.get("http://localhost:8080/brand-case/user/qq_login", {
                    params: {
                        qqNumber: this.qqNumber,
                        verificationCode: this.verificationCode
                    }
                }).then(resp => {
                    if (resp.data === "success") {
                        this.msg = "登录成功，3s后将跳转到您的主页";
                        setTimeout(() => {
                            // 这里添加跳转到主页的代码
                            location.href="brand2.html";
                        }, 3000);
                    } else {
                        this.msg = "登录失败，您输入的验证码有误！";
                    }
                }).catch(error => {
                    console.error("登录失败：", error);
                    this.msg = "登录失败，请检查网络或联系管理员。";
                });
            },
            tologin(){
                location.href="login.html";
            }
        }
    });
</script>
</body>
</html>